package net.viralpatel.struts2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class ListUserAction extends ActionSupport{
	private List<User> listUser = new ArrayList<User>();
	public String execute() throws Exception {
		String url = "jdbc:mysql://localhost:3306/";
		String dbName = "t10";
		String driverName = "com.mysql.jdbc.Driver";
		Connection con = null;
		ResultSet resultSet = null;
		String nameroot = "root";
		String passwordroot = "";
		try {
			Class.forName(driverName);
			con = DriverManager.getConnection(url + dbName, nameroot,
					passwordroot);
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		String selectListUser = "SELECT userID, fullname, mail, tel, company, priority FROM user WHERE priority = 0";
		PreparedStatement prepStmt = con.prepareStatement(selectListUser);
		resultSet = prepStmt.executeQuery();
		if (resultSet.next()) {
			resultSet.beforeFirst();
			while (resultSet.next()) { // process results one row at a time
				
				if(resultSet.getInt(6)==0){
					User user = new User(resultSet.getString(1), resultSet
							.getString(2), resultSet.getString(3), resultSet
							.getString(4), resultSet.getString(5), resultSet
							.getInt(6));
					listUser.add(user);
				}
			}
			con.close();
			return SUCCESS;
		}else{
			addActionError(getText("error.listUser"));
			con.close();
			return ERROR;
		}	
	}
	public List<User> getListUser(){
		return listUser;
	}
	public void setListUser(List<User> listUser){
		this.listUser=listUser;
	}
	
}
